<?php
/**
 * Yotaku Shop - yshop
 * @author Creatint<creatint@163.com>
 * @copyright www.yotaku.cn 2016
 */
namespace Admin\Controller;
use Admin\Model\OrderModel;
class OrderController extends AdminController {
    public function index(){
        $param = I("get.");
        $Order   =   new OrderModel();
        $orders   =   $Order->getOrder($param,false);

        foreach($orders as $k=>&$v){
            $v['cartdata'] = json_decode($v['cartdata'],true);
//            $cartdata = json_decode($v['cartdata'],true);
            foreach($v['cartdata']["item"] as $key=>$item){

                if(isset($item['tags'])) $orders[$k]['cartdata']["item"][$key]['tags'] = json_decode($item['tags'], true);
//                $v['goods'][] = $tiem;
//                $orders[$k]['cartdata']["item"][$key]['tags'] = null;
            }unset($item);
        }unset($v);
//        dump($orders);

//        trace($orders);
        $this->assign('orders',$orders);
        $this->display();
    }

    public function update(){
        $data = array();
        $data['cartdata']   =   '{"item":[{"id":1,"cover":1,"title":"\u8863\u54c1\u5929\u6210 2016\u79cb\u88c5\u65b0\u54c1\u7537\u88c5\u9488\u7ec7\u5f00\u886b \u97e9\u7248\u6761\u7eb9\u65f6\u5c1a\u9752\u5e74\u6bdb\u8863\u5916\u5957","price":178,"count":1},{"id":2,"cover":2,"title":"\u8863\u670d\u8863\u670d","price":100,"count":2}]}';//sdf
        $data['id'] =   1;
        $Order = D("Order");
        $Order->update($data);
    }

    //操作订单
    public function operate(){
        $data = I("get.");
        $Order = D("Order");
        if($Order->update($data)){
            $this->success('操作成功');
        }else{
            $this->error('操作失败：'.$Order->getError());
        }
    }
    //发货
    public function deliver(){
        $data['wuliu'] = I("post.wuliu",'');
        $data['wuliu_number'] = I("post.wuliu_number",0);
        $data['deliver_status'] = '1';
        $data['id'] = I("get.id");
        $data = array_filter($data);

        if(empty($data) || !($data['id']>0) || empty($data['wuliu']) || empty($data['wuliu_number'])){ 
            $res = array('code'=>0,'msg'=>'物流信息不完整');
        }else{
            $Order = D("Order");
            if($Order->update($data)){
                $res = array('code'=>1,'msg'=>'操作成功');
            }else{
                $res = array('code'=>0,'msg'=>'操作失败');
            }
        }


        $this->ajaxReturn($res);
    }

    //获取物流信息
    public function get_deliver($id){
        if(!$id){
            return '';
        }
        $wuliu = get_order_wuliu($id);
        $this->ajaxReturn($wuliu);
    }

    //获取快递公司
    public function get_deliver_com(){
        $com_name = I("com_name",'');
        $com = get_order_wuliu_com($com_name);
        $this->ajaxReturn($com);
    }

    //删除
    public function delete($id){
        $Order = D("Order");
        if($res = $Order->del($id)){
            $this->success('删除成功');
        }else{
            $this->error($Order->getError());
        }
    }
}